# -*- coding: utf-8 -*-
"""
Created on Mon Sep  6 12:00:04 2021

@author: xiong
"""
import os
from PIL import Image
def getAllImg(path):
     result = []
     filelist = os.listdir(path)
     for file in filelist:
         if os.path.isfile(os.path.join(path, file)):
             if file.split('.')[1] in ('jpg', 'png'):
                 result.append(os.path.join(path, file))
     return result
 

def join(png1, png2, path):
     img1, img2 = Image.open(png1), Image.open(png2)
     img1 = img1.resize(picsize)
     img2 = img2.resize(picsize)
     joint = Image.new('RGB', (picsize[0]+picsize[0], picsize[1]))
     # 新建一张新的图片
     # 因为拼接的图片的宽都是一样，所以宽为固定值
     # 高为两张图片的高相加再减去需要缩进的部分
     loc1, loc2 = (0, 0), (picsize[0], 0)
     joint.paste(img2, loc2)
     joint.paste(img1, loc1)
     joint.save(path+'\\' + ''.join(png2.split('_')[-3:]))
     print(''.join(png2.split('_')[-3:]) + '生成')
     
     
     
path = r'E:\tutu\有序图'
#    就改这一个变量，分布图、有序图都放在这个目录里面，
#    分布图名字格式xxxxx分布_地域1，有序图名字格式xxxxx分布_地域1_类型1
#    路径中不能有 ’类型‘ 跟’地域‘这俩词
proportion = 0.3 #跟原图的比例，越小速度越快，但是太小会不清晰



picList = getAllImg(path)  
comparePath = path + '\\compare' 
try:
    os.mkdir(comparePath)
except:
    pass
picsize = (int(7440*proportion), int(6375*proportion))

temprDic = {}
tempr = ['交错带','高山极地带', '苔原带', '寒温带', '中温带', '暖温带', '亚热带', '热带','热带','热带']
temprList = []
for i in picList:
    if '类型' not in i and '地域' in i:
        tempri = i.split('_')[-2]
        typei = 0
        areai = int(i.split('_')[-1][(i.split('_')[-1].index('地域')+2) : i.split('_')[-1].index('.')])
        if tempri in temprList:
            temprDic[tempri][areai][typei] = i
        else:
            temprDic[tempri] = [[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]] for e in range(40)]
            temprDic[tempri][areai][typei] = i
            temprList.append(tempri)
    elif '类型'  in i and '地域' in i:
        tempri = i.split('_')[-3]
        typei = int(i.split('_')[-1][(i.split('_')[-1].index('类型')+2) : i.split('_')[-1].index('.')])
        areai = int(i.split('_')[-2][(i.split('_')[-2].index('地域')+2):])
        temprDic[tempri][areai][typei] = i

for i in temprList:
    for j in temprDic[i]:
        if j:
            for e in j[1:]:
                if e:
                    join(j[0],e, comparePath)
            
            

    
    
                    